﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CollegeShop.Repository.Win.UserControl;
using DevExpress.XtraLayout;
using System.IO;
using CollegeShop.Api.Framework;
using CollegeShop.Repository.Win.CSWS;
using CollegeShop.Repository.Win.ViewObject;

namespace CollegeShop.Repository.Win.BaseData
{
    public partial class F_GoodsInput : DevExpress.XtraEditors.XtraForm
    {
        public static string rich = string.Empty;

        RichEdit richEdit = new RichEdit();
        private static int pageNo = 0;//当前页
        private static int pageSize=10;//显示多少个
        private static byte[] imagesUrl = null;
        private static CommodityVO model = new CommodityVO();
        private static CollegeShopAPISoapClient api = new CollegeShopAPISoapClient();
        public F_GoodsInput()
        {
            InitializeComponent();
            InitData();
            this.richText.AddItem("richEdit", richEdit);
            this.richText.Items[0].TextVisible = false;

            model.Id = -1;
        }

        public F_GoodsInput(int id)
        {
            InitializeComponent();
            InitData();
            this.richText.AddItem("richEdit", richEdit);
            this.richText.Items[0].TextVisible = false;
            model.Id = id;
        }

        #region 数据初始化
        private void InitData()
        {
            string category = api.GetCommodityType(Program.college);

            List<CommodityTypeVO> commodityTypes = TypePlug.ToObject<List<CommodityTypeVO>>(category);
            this.lupe_goodsCategory.Properties.DisplayMember = "Name";
            this.lupe_goodsCategory.Properties.ValueMember = "Id";
            this.lupe_goodsCategory.EditValue = "Id";
            this.lupe_goodsCategory.Properties.PopulateColumns();
            this.lupe_goodsCategory.Properties.DataSource = commodityTypes;
            this.lupe_goodsCategory.ItemIndex = 0;
            this.lupe_goodsCategory.Properties.NullText = "--请选择--";
            this.lupe_goodsCategory.Properties.ShowLines = false;
            this.lupe_goodsCategory.Properties.ShowHeader = false;

            if (commodityTypes != null && commodityTypes.Count != 0)
            {
                InitCommodity(commodityTypes[0].Id);
            }
        }

        private void InitCommodity(int commodityTypeId) {
            List<CommodityVO> commodities = TypePlug.ToObject<List<CommodityVO>>(api.GetCommodities(Program.college, commodityTypeId, pageNo, pageSize));
            if (commodities == null || commodities.Count == 0) {
                commodities.Add(new CommodityVO());
            }
           
        }
        #endregion

        

        #region 增删改

        private void SaveGoods()
        {
            string msg = "添加";
            if (model.Id != -1)
            {
                msg = "修改!";
            }
            try
            {
                if (!validate())
                    return;
                CommodityVO commodityVO = InitCommodityVO(model.Id);
                if (Convert.ToBoolean(api.SaveCommodity(Program.college, commodityVO)))
                {
                    reSet();
                }
                else
                {
                    XtraMessageBox.Show(msg + "商品失败！", "温馨提示", MessageBoxButtons.OK);
                }

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(msg + "商品失败！", "温馨提示", MessageBoxButtons.OK);
            }
        }

        private void DelGoods()
        {
            try {
                if (model.Id == -1) {
                    XtraMessageBox.Show("没有要删除的商品！", "温馨提示", MessageBoxButtons.OK);
                   
                }
                if (!Convert.ToBoolean(api.DelCommodity(Program.college, model.Id.ToString(), model.ResourceID.ToString())))
                {
                    XtraMessageBox.Show("删除商品失败！", "温馨提示", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex) { 
            
            }
        }

        private CommodityVO InitCommodityVO(int id) {
            CommodityVO commodityVo = new CommodityVO();
            commodityVo.Code = this.txt_goodsCode.Text.Trim();
            commodityVo.Description = rich;
            commodityVo.ImagesUrl = imagesUrl;
            commodityVo.Name = this.txt_goodsName.Text.Trim();
            commodityVo.NamePY = this.txt_goodsNamepy.Text.Trim();
            commodityVo.PackStandard = Convert.ToInt32(this.txt_packStandard.Text.Trim());
            commodityVo.Price = this.txt_defaultSalePrice.Text.Trim();
            commodityVo.Standard = this.txt_standard.Text.Trim();
            commodityVo.Unit = this.txt_unit.Text.Trim();
            commodityVo.Title = this.txt_goodsTitle.Text.Trim();
            commodityVo.Type = Convert.ToInt32(this.lupe_goodsCategory.EditValue);
            commodityVo.TypeName = this.lupe_goodsCategory.Text.Trim();
            commodityVo.State = 1;
            commodityVo.Id = id;
            return commodityVo;
        }

        private bool validate() {
            if (imagesUrl == null) {
                XtraMessageBox.Show("图片不能为空！", "温馨提示", MessageBoxButtons.OK);
                return false;
            }
            return true;
        }
        private void reSet() {
            this.txt_goodsCode.Text = "";
            this.txt_goodsName.Text = "";
            this.txt_goodsNamepy.Text = "";
            this.txt_packStandard.Text = "";
            this.txt_defaultSalePrice.Text = "";
            this.txt_unit.Text = "";
            this.txt_standard.Text = "";
            this.txt_goodsTitle.Text = "";
            this.lupe_goodsCategory.ItemIndex = 0;
            this.lupe_goodsCategory.Text = "";
            imagesUrl = null;
            rich = "";
        }
        #endregion

        private void sb_imgUpload_Click(object sender, EventArgs e)
        {
            OpenFileDialog sf = new OpenFileDialog();
            sf.FileOk += new CancelEventHandler(this.UploadImage);
            sf.ShowDialog();
            sf.Dispose();
        }

        private void UploadImage(object sender, EventArgs e)
        {
            OpenFileDialog sf = (OpenFileDialog)sender;
            this.pictureEdit.Image = Image.FromFile(sf.FileName);
            FileInfo fi = new FileInfo(sf.FileName);
            imagesUrl = new byte[fi.Length];
            
            System.IO.FileStream fs = fi.OpenRead();
            fs.Read(imagesUrl, 0, Convert.ToInt32(fi.Length));
            //new CollegeShopAPISoapClient().UploadImage(b, "abc" + System.Guid.NewGuid() + ".jpg");
        }

        private void sb_save_Click(object sender, EventArgs e)
        {
            SaveGoods();
        }

        private void sb_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.txt_defaultSalePrice.Text = "";
            this.txt_goodsCode.Text = "";
            this.txt_goodsName.Text = "";
            this.txt_goodsNamepy.Text = "";
            this.txt_goodsTitle.Text = "";
            this.txt_packStandard.Text = "";
            this.txt_standard.Text = "";
            this.txt_unit.Text = "";
        }


    }
}